//
//  UIView+RadiusImage.m
//  TravelGuideHD
//
//  Created by 刘澈 on 14-9-23.
//  Copyright (c) 2014年 mafengo Inc. All rights reserved.
//

#import "UIView+RadiusImage.h"
#import "NSObject+Associative.h"

@implementation UIView (RadiusImage)


const char* radiusImageViewKey = "Radius_ImageView_Key";

static const char UIViewRadiusImage;

- (BOOL)isRadiusImage
{
    NSNumber *n = [self getAssociativeValue:&UIViewRadiusImage];
    return  n.boolValue;
}

- (void)setIsRadiusImage:(BOOL)isRadiusImage
{
    NSNumber *n = [NSNumber numberWithFloat:isRadiusImage];
   [self setRetainNonatomicProperty:n byKey:&UIViewRadiusImage];
    [self removeImageView];
    if(isRadiusImage){
        [self createImageView];
    }
}

-(void)createImageView{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth;
    UIImage *image = [[UIImage imageNamed:@"img_circle_cover"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch];
    imageView.image = image;
    [self setRadiusImageView:imageView];
    [self addSubview:imageView];
}

-(void)removeImageView{
    UIImageView *imageView = [self getRadiusImageView];
    if(imageView != nil){
        [imageView removeFromSuperview];
    }
    [self setRadiusImageView:nil];
}

-(void)setRadiusImageView:(UIImageView *)radiusImageView {
    objc_setAssociatedObject(self,&radiusImageViewKey,radiusImageView,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIImageView *)getRadiusImageView{
    return objc_getAssociatedObject(self,&radiusImageViewKey);
}



@end
